/**
 * 应用配置文件
 * Application Configuration
 */

const path = require('path');
require('dotenv').config({ path: path.join(__dirname, '../../.env') });

const config = {
  // 数据库配置 / Database Configuration
  database: {
    host: process.env.DB_HOST || 'localhost',
    port: parseInt(process.env.DB_PORT) || 3306,
    database: process.env.DB_NAME || 'personal_cloud_notes',
    user: process.env.DB_USER || 'notes_user',
    password: process.env.DB_PASSWORD || 'WJ20240824zyq',
    connectionLimit: parseInt(process.env.DB_CONNECTION_LIMIT) || 10,
    acquireTimeout: parseInt(process.env.DB_ACQUIRE_TIMEOUT) || 60000,
    timeout: parseInt(process.env.DB_TIMEOUT) || 60000,
    reconnect: true,
    // 连接池优化配置
    queueLimit: parseInt(process.env.DB_QUEUE_LIMIT) || 0,
    idleTimeout: parseInt(process.env.DB_IDLE_TIMEOUT) || 300000, // 5分钟
    maxReconnects: parseInt(process.env.DB_MAX_RECONNECTS) || 3,
    reconnectDelay: parseInt(process.env.DB_RECONNECT_DELAY) || 2000,
    // 性能优化
    multipleStatements: false,
    dateStrings: false,
    supportBigNumbers: true,
    bigNumberStrings: false
  },

  // JWT配置 / JWT Configuration
  jwt: {
    secret: process.env.JWT_SECRET || 'your_super_secret_jwt_key_here',
    expiresIn: process.env.JWT_EXPIRES_IN || '7d'
  },

  // 服务器配置 / Server Configuration
  server: {
    port: parseInt(process.env.PORT) || 3000,
    env: process.env.NODE_ENV || 'development'
  },

  // 文件上传配置 / File Upload Configuration
  upload: {
    path: process.env.UPLOAD_PATH || './data/users',
    tempPath: process.env.UPLOAD_PATH ? `${process.env.UPLOAD_PATH}/../temp` : './data/temp',
    maxFileSize: parseInt(process.env.MAX_FILE_SIZE) || 100 * 1024 * 1024, // 100MB
    maxImageSize: parseInt(process.env.MAX_IMAGE_SIZE) || 5 * 1024 * 1024, // 5MB
    maxVideoSize: parseInt(process.env.MAX_VIDEO_SIZE) || 50 * 1024 * 1024, // 50MB
    maxAudioSize: parseInt(process.env.MAX_AUDIO_SIZE) || 10 * 1024 * 1024, // 10MB
    allowedImageTypes: ['image/jpeg', 'image/png', 'image/webp', 'image/gif'],
    allowedVideoTypes: ['video/mp4', 'video/webm', 'video/ogg'],
    allowedAudioTypes: ['audio/mpeg', 'audio/wav', 'audio/ogg', 'audio/mp4'],
    allowedDocumentTypes: [
      'application/pdf',
      'application/msword',
      'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
      'application/vnd.ms-excel',
      'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
      'text/plain',
      'text/csv'
    ]
  },

  // 存储限制配置 / Storage Limit Configuration
  storage: {
    defaultLimit: parseInt(process.env.DEFAULT_STORAGE_LIMIT) || 30 * 1024 * 1024 * 1024, // 30GB
    warningThreshold: 0.8, // 80%警告阈值
    maxThreshold: 0.95 // 95%最大阈值
  },

  // 安全配置 / Security Configuration
  security: {
    bcryptRounds: parseInt(process.env.BCRYPT_ROUNDS) || 10,
    rateLimitWindowMs: parseInt(process.env.RATE_LIMIT_WINDOW_MS) || 15 * 60 * 1000,
    rateLimitMaxRequests: parseInt(process.env.RATE_LIMIT_MAX_REQUESTS) || 100,
    sessionTimeout: 7 * 24 * 60 * 60 * 1000 // 7天
  },

  // 备份配置 / Backup Configuration
  backup: {
    path: process.env.BACKUP_PATH || './data/backups',
    retentionDays: parseInt(process.env.BACKUP_RETENTION_DAYS) || 7,
    schedule: '0 2 * * *' // 每天凌晨2点执行备份
  },

  // 日志配置 / Logging Configuration
  logging: {
    level: process.env.LOG_LEVEL || 'info',
    path: process.env.LOG_PATH || './data/logs',
    maxSize: process.env.LOG_MAX_SIZE || '10m',
    maxFiles: process.env.LOG_MAX_FILES || '7d',
    rotationInterval: process.env.LOG_ROTATION_INTERVAL || '1d',
    enableConsole: process.env.LOG_ENABLE_CONSOLE !== 'false',
    enableFile: process.env.LOG_ENABLE_FILE !== 'false'
  },

  // 性能监控配置 / Performance Monitoring Configuration
  performance: {
    enableMonitoring: process.env.ENABLE_PERFORMANCE_MONITORING !== 'false',
    metricsInterval: parseInt(process.env.METRICS_INTERVAL) || 30000, // 30秒
    alertThresholds: {
      memoryUsage: parseInt(process.env.MEMORY_ALERT_THRESHOLD) || 80,
      cpuUsage: parseInt(process.env.CPU_ALERT_THRESHOLD) || 80,
      responseTime: parseInt(process.env.RESPONSE_TIME_ALERT_THRESHOLD) || 5000,
      storageUsage: parseInt(process.env.STORAGE_ALERT_THRESHOLD) || 90
    },
    enableSlowQueryLog: process.env.ENABLE_SLOW_QUERY_LOG !== 'false',
    slowQueryThreshold: parseInt(process.env.SLOW_QUERY_THRESHOLD) || 1000 // 1秒
  },

  // 缓存配置 / Cache Configuration
  cache: {
    enableStaticCache: process.env.ENABLE_STATIC_CACHE !== 'false',
    staticCacheMaxAge: parseInt(process.env.STATIC_CACHE_MAX_AGE) || 86400, // 1天
    enableGzip: process.env.ENABLE_GZIP !== 'false',
    gzipLevel: parseInt(process.env.GZIP_LEVEL) || 6,
    enableBrotli: process.env.ENABLE_BROTLI !== 'false'
  },

  // 图片压缩配置 / Image Compression Configuration
  imageCompression: {
    quality: 80,
    maxWidth: 1920,
    maxHeight: 1080,
    format: 'webp'
  }
};

module.exports = config;